<template>
 <div class="indexTable bgWhite productType" id="article_comment" ref="content">
   <el-form ref="form" class="pb_form">
      <el-select  v-model="searchForm.article_title.value" placeholder="文章标题" filterable clearable  @keyup.enter.native="getTableList" @focus="getTitleList" >
        <el-option v-for="item in titleList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-select  v-model="searchForm.first_type.value" placeholder="一级类型"  clearable  @keyup.enter.native="getTableList" @focus="getFirstTypeList" >
        <el-option v-for="item in firstTypeList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <el-input v-model="searchForm.nickname.value" placeholder="用户昵称" clearable  @keyup.enter.native="getTableList"> </el-input>
      <el-select  v-model="searchForm.country.value" placeholder="国家" clearable  @keyup.enter.native="getTableList" @focus="getNameEnList" >
        <el-option v-for="item in nameEnList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
      <div class="createTime">
         <el-input v-model="commentTime" placeholder="评论当地日期"></el-input>
         <el-date-picker @change="changeCommentTime"   v-model="commentTimePicker"
          type="datetimerange" range-separator="至"  :default-time="['00:00:00' , '23:59:59']" 
         start-placeholder="开始日期"  value-format="yyyy-MM-dd HH:mm:ss" end-placeholder="结束日期" placeholder="选择操作时间"></el-date-picker>
      </div>
      <div class="createTime">
         <el-input v-model="createTime" placeholder="创建日期"></el-input>
         <el-date-picker @change="changeCreateTime"   v-model="createTimePicker"
          type="datetimerange" range-separator="至"  :default-time="['00:00:00' , '23:59:59']" 
         start-placeholder="开始日期"  value-format="yyyy-MM-dd HH:mm:ss" end-placeholder="结束日期" placeholder="选择操作时间"></el-date-picker>
      </div>
      <el-select  v-model="searchForm.comment_sort.value" placeholder="评论排序" filterable clearable  @keyup.enter.native="getTableList" @focus="getTagTypeList" >
        <el-option v-for="item in sortList" :key="item.id" :value="item.value" :label="item.label"></el-option>
      </el-select>
    </el-form>
    <div class="btnContainer" ref="btn">
      <div>
        <el-button type="del" @click="sureCloseDialog('sort')" v-if="$_has('/article_comment/set_sort')">排序</el-button>
        <el-button type="del" @click="sureCloseDialog('del')" v-if="$_has('/article_comment/del')">删除</el-button>
        <el-button type="del" @click="closeExportDialog()" v-if="$_has('/article_comment/export')">导出</el-button>
      </div>
      <div>
        <el-button type="search" @click="getTableList" v-if="$_has('/article_comment/list')">查询</el-button>
        <el-button type="del" @click="resetForm">重置</el-button>
      </div>
    </div>
    <el-table :data="listTable" :height="tableH" :row-class-name="rowClassName" ref="table" @row-click="RowClick"
     @selection-change="handleSelectionChange" :row-key="function(row){return row.id}"  v-loading="loading">
      <el-table-column type="selection" width="64"  fixed="left" :reserve-selection="true"></el-table-column>
      <el-table-column  label="文章标题" width="400" align="left"  prop="article_title"></el-table-column>
      <el-table-column  label="一级类型" width="200"   prop="first_type"></el-table-column>

      <el-table-column  label="用户昵称" width="200"   prop="nickname"></el-table-column>
      <el-table-column  label="国家" width="200" align="left" prop="country"></el-table-column>
      <el-table-column  label="评论当地日期" width="200"  prop="local_time"></el-table-column>
      <el-table-column  label="评论内容" width="200" align="left"  prop="content"></el-table-column>
      <el-table-column  label="回复详情" width="200"  >
        <template slot-scope="scope" >
          <div  class="content_details"  @click.stop.prevent="setCurRow(scope.row, 'details')">
            <i class="iconfont icon-details_normal"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column  label="评论排序" width="200"  prop="comment_sort"></el-table-column>
      <el-table-column  label="创建时间" width="200"   prop="create_time"></el-table-column>
      <el-table-column  label="操作" fixed="right"  width="160" class-name="options">
        <template slot-scope="scope" >
            <i class="iconfont  icon-delete_normal" v-if="$_has('/article_comment/del')" @click.stop.prevent="setCurRow(scope.row, 'del')" ></i>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination ref="pagination" background :page-sizes="pageSizes" :total="total"  :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"  @size-change="handleSizeChange" :current-page="page"
      @current-change="handleCurrentChange"></el-pagination>

    <Del v-show="delShow" :idList="curList" :show="delShow" @closeDialog="closeDelDialog"></Del>
    <Sort v-show="sortShow" :idList="curList" :show="sortShow" @closeDialog="closeSortDialog"></Sort>
    <Export v-show="exportShow" :show="exportShow" @closeDialog="closeExportDialog"></Export>
    <Details v-show="detailShow" :show="detailShow" @closeDialog="closeDeTailDialog"></Details> 
 </div>
</template>

<script>
import { list } from '@/api/article/article_comment.js'
import { articleManageTitle } from '@/api/article/article_manage.js'
import { nameEnList } from '@/api/manage/site.js'
import { firstTypeList } from '@/api/article/article_type.js'
import { setSearchParams } from '@/utils/index.js'
import { Del, Sort, Export, Details } from './components'
import { tagTypeList } from '@/api/article/article_tag_type.js'
export default {
  data () {
    return {
      searchForm: {
        article_title: { value: '', op: '=' },
        first_type: { value: '', op: '=' },
        nickname: { value: '', op: 'like' },
        country: { value: '', op: '=' },
        local_time: { value: '', op: 'RANGE' },
        create_time: { value: '', op: 'RANGE' },
        comment_sort:  { value: '', op: '=' }
      },
      titleList:[],
      listTable: [  ],
      pageSizes: [20, 50, 100],
      total: 10,
      pageSize: 20,
      page: 1,
      delShow: false,
      sortShow: false,
      exportShow: false,
      tableH: null,
      curList: [],
      firstTypeList: [],
      sortList: [
        {value: '1', label: 'S'},
        {value: '2', label: 'A'},
        {value: '3', label: 'B'},
        {value: '4', label: 'C'},
        {value: '5', label: 'D'},
      ],
      nameEnList: [],

      commentTimePicker: '',
      commentTime: '',
      createTime: '',
      createTimePicker: '',
      detailShow: false
    }
  },
  components: { Del, Sort, Export, Details },
  mounted(){
    let that = this;
    that.setTableHeight()
    // 注：window.onresize只能在项目内触发1次
    window.onresize = function windowResize () {
        // 通过捕获系统的onresize事件触发我们需要执行的事件
      that.setTableHeight()
    }
  },
  created () {
    this.getTableList()
  },
  methods: {

    closeDeTailDialog () {
      this.detailShow =  !this.detailShow
    },
    changeCommentTime (arr) {
      if(arr){
        let str = arr[0] + ' - ' + arr[1]
        this.commentTime = arr[0].split(' ')[0] +' - '+arr[1].split(' ')[0]
        this.searchForm.local_time.value = str
      }else{
        this.commentTime  = ""
        this.searchForm.local_time.value = null
      }
      this.getTableList()
    },
    changeCreateTime (arr) {
      if(arr){
        let str = arr[0] + ' - ' + arr[1]
        this.createTime = arr[0].split(' ')[0] +' - '+arr[1].split(' ')[0]
        this.searchForm.create_time.value = str
      }else{
        this.createTime = ''
        this.searchForm.create_time.value = null
      }
      this.getTableList()
    },
    getNameEnList(){ // 国家英文名称
      nameEnList().then(res=>{
        this.nameEnList = res.data
      })
    },
    getTagTypeList () {
      tagTypeList().then(res=>{
        this.tagTypeList = res.data
      })
    },
    getTitleList () {
      articleManageTitle().then(res=>{
        this.titleList = res.data
      })
    }, 
    closeDiscountShowDialog () {
      this.discountShow = !this.discountShow
    },
    closeExportDialog(){
      this.exportShow = !this.exportShow
      if(!this.exportShow){
        this.getTableList()
      }
    },
    closeSortDialog(){
      this.sortShow = !this.sortShow
      if(!this.sortShow){
        this.getTableList()
      }
    },
    sureCloseDialog(type){
      if(this.curList.length > 0){
        if(type == 'del'){
          this.closeDelDialog()
        } else if (type == 'sort'){
          this.closeSortDialog()
        }
      } else {
        let word = "删除"
        if(type == 'sort'){
          word = '排序'
        }
        this.$message.error('请选择需' + word + '的选项！')
      }
      
    },
    rowClassName({row}){
      let rowClass = "tableRowClass"
      if(row.statusSelection){
        rowClass += ' selectRow'
      }
      return rowClass
    },
    setCurRow(row, type) {
      this.curList = []
      this.curList.push(row.id)
      this.curId = row.id
      if(type == 'edit'){ 
        this.closeEditDialog()  
      }else if(type == 'del'){
        this.closeDelDialog()
      } else if (type == 'details') {
        this.closeDeTailDialog()
      }
      
    },
    RowClick (row) {
      this.$refs.table.toggleRowSelection(row);
    } ,
    handleSelectionChange(list){
      let array = []
      for(let i = 0; i < list.length; i++ ){
        array.push(list[i].id)
      }
      this.curList = array
      // 选中行的样式赋值
      for(let i in this.selectList){
        this.selectList[i].statusSelection = false
      }
      for (let item in list){
        list[item].statusSelection = true
      }
      this.selectList = list
    },
    getTableList(){
      this.loading = true
      let params = setSearchParams(this.searchForm, this.pageSize, this.page)
      // 发请求的位置
      list(params).then(res=>{
          let list = res.data.data
        if( list.length == 0 && this.page > 1){
          this.page -= 1
          this.getTableList()
        }
        let  idList = []
        for(let i = 0; i < list.length; i++){

          if(list[i].is_top == 1) {
            list[i].is_top_status = true
          } else {
            list[i].is_top_status = false
          }
          idList.push(list[i].id)
        }
        this.curIdList = idList

        this.listTable = list
        this.total = res.data.total
        this.page = res.data.current_page
        this.$refs.table.clearSelection()
        this.loading = false
      })
    },
    closeAddDialog(){
      this.addShow = !this.addShow
      if(!this.addShow){
        this.getTableList()
      }
    },
    closeEditDialog(){
      this.editShow = !this.editShow
      if(!this.editShow){
        this.getTableList()
      }
    },
    closeDelDialog(){
      this.delShow = !this.delShow
      if(!this.delShow){
        this.getTableList()
        this.curList = []
      }
    },
   
    getFirstTypeList(){
      firstTypeList().then(res=>{
        this.firstTypeList = res.data
      })
    },
    setTableHeight(){
      let searchFormH = this.$refs.form.$el.offsetHeight;
      let manageMenuH = this.$refs.content.offsetHeight;
      let btnContainerH = this.$refs.btn.offsetHeight;
      let paginationH = this.$refs.pagination.$el.offsetHeight
      let tableHeight = manageMenuH  - btnContainerH - searchFormH - paginationH - 48 - 24 - 24
      this.tableH = tableHeight +'px' 
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getTableList()
      this.$refs.table.clearSelection()
    },
    resetForm(){
      for(let item in this.searchForm){
        this.searchForm[item].value =""
      }
      this.commentTime = ""
      this.commentTimePicker = ''
      this.createTime = ""
      this.createTimePicker = ''
      this.getTableList()
    },
  }

}
</script>

<style lang="scss">
@import '@/styles/article/article_comment.scss';
</style>